/**
 * 初始化蓝牙适配器
 */
function e(e) {
  wx.openBluetoothAdapter({
    success: function(o) {
      console.log("初始化蓝牙适配器成功"), c(e);
    },
    fail: function(c) {
      e.onFailCallBack("蓝牙功能不可用");
    },
    complete: function(e) {
      console.log(e);
    }
  });
}

/**
 * 开启蓝牙搜索模块
 */
function c(e, c) {
  k = !1, void 0 === c && (c = function(e) {}), U = c, setTimeout(function() {
    return k ? void 0 : (console.log("搜索设备超时"), e.onFailCallBack("搜索设备超时"), void clearInterval(D));
  }, 6e3), wx.startBluetoothDevicesDiscovery({
    success: function(c) {
      console.log("开启搜索成功"), o(e);
    },
    fail: function(c) {
      console.log("开启搜索失败"), e.onFailCallBack(c);
    },
    complete: function(e) {
      console.log(e);
    }
  }), D = setInterval(function() {
    0 == k ? o(e) : clearInterval(D);
  }, 1e3);
}

/**
 * 获取设备列表
 */
function o(e) {
  wx.getBluetoothDevices({
    success: function(c) {
      console.log(c);
      for (var o = 0; o < c.devices.length; o++) {
        console.log("res.devices[i].name", c.devices[o].name);
        if (w.isContains(c.devices[o].name, e.name)) { //"AKENO_57E481")) {
          if (console.log("发现设备" + c.devices[o].name), i(), k = !0, clearInterval(D), g = c.devices[o],
            console.log("信号强度" + c.devices[o].RSSI), console.log("params.sendCommend=" + e.sendCommend),
            void 0 === e.sendCommend) return void e.onSuccessCallBack(c);
          if ("1001" == e.sendCommend) return void e.onSuccessCallBack(c.devices[o].RSSI);
          n(e);
        }
      }
    },
    fail: function(c) {
      clearInterval(D), console.log("没有搜索到要链接的设备...."), console.log(c), e.onFailCallBack(c),
        i();
    }
  });
}

/**
 * 停止搜索蓝牙设备
 */
function i() {
  wx.stopBluetoothDevicesDiscovery({
    success: function(e) {
      console.log(e);
    }
  });
}

/**
 * 连接设备
 */
function n(e) {
  setTimeout(function() {
    S || (console.log("连接设备超时"), e.onFailCallBack("连接设备超时"));
  }, 5e3), wx.createBLEConnection({
    deviceId: g.deviceId + "",
    success: function(c) {
      console.log("连接成功 : " + g.deviceId), S = !0, t(e);
    },
    fail: function(c) {
      console.log("连接失败 : " + g.deviceId), e.onFailCallBack(c);
    },
    complete: function(e) {
      console.log(e);
    }
  });
}

/**
 * 监听蓝牙设备状态
 */
function s(e) {
  wx.onBLEConnectionStateChange(function(e) {
    return console.log("device " + e.deviceId + " state has changed, connected: " + e.connected),
      e.connected;
  });
}

/**
 * 获取notify服务
 */
function t(e) {
  wx.getBLEDeviceServices({
    deviceId: g.deviceId + "",
    success: function(c) {
      console.log("获取服务成功 :"), console.log("device services:", c.services), console.log("params.writeServiceUUID=" + e.writeServiceUUID),
        console.log("params.notifyServiceUUID=" + e.notifyServiceUUID);
      for (var o = 0; o < c.services.length; o++) console.log("res.services[" + o + "].uuid=" + c.services[o].uuid),
        c.services[o].uuid == e.writeServiceUUID && (console.log("writeService=" + c.services[o].uuid),
          C = c.services[o]), c.services[o].uuid == e.notifyServiceUUID && (console.log("notifyService=" + c.services[o].uuid),
          I = c.services[o]);
      l(e);
    }
  });
}

/**
 * 获取write服务
 */
function l(e) {
  console.log(g.deviceId), console.log(I.uuid), wx.getBLEDeviceCharacteristics({
    deviceId: g.deviceId + "",
    serviceId: I.uuid + "",
    success: function(c) {
      console.log("唤醒特征值获取成功："), console.log("device getBLEDeviceCharacteristics:", c.characteristics);
      for (var o = 0; o < c.characteristics.length; o++) c.characteristics[o].uuid == e.notifyCharacteristicUUID && (B = c.characteristics[o]);
      console.log("唤醒特征值 :", B), r(e);
    },
    fail: function(c) {
      console.log("唤醒特征值获取失败：", c), e.onFailCallBack("唤醒特征值获取失败");
    }
  });
}

/**
 * 向write服务发送写入命令
 */
function r(e) {
  wx.getBLEDeviceCharacteristics({
    deviceId: g.deviceId + "",
    serviceId: C.uuid + "",
    success: function(c) {
      console.log("params.writeCharacteristicUUID=" + e.writeCharacteristicUUID);
      for (var o = 0; o < c.characteristics.length; o++) console.log("res.characteristics[i].uuid=" + c.characteristics[o].uuid),
        c.characteristics[o].uuid == e.writeCharacteristicUUID && (h = c.characteristics[o],
          console.log("writeCharacteristic", h));
      console.log("xieru :", h), a(e);
    },
    fail: function(c) {
      console.log("写入特征值获取失败：", c), d(e.sendCommend, e.onSuccessCallBack, e.onFailCallBack);
    }
  });
}

/**
 * 监听特征值变化
 */
function a(e) {
  wx.notifyBLECharacteristicValueChanged({
    deviceId: g.deviceId + "",
    serviceId: I.uuid + "",
    characteristicId: B.uuid + "",
    state: !0,
    success: function(c) {
      console.log("开启监听成功" + c.errMsg), setTimeout(function() {
        U("ok"), d(e.sendCommend, e.onSuccessCallBack, e.onFailCallBack);
      }, 1000);
    },
    fail: function(c) {
      console.log("开启监听失败" + c.errMsg), e.onFailCallBack("开启监听失败");
    }
  }), u();
}

/**
 * 特征值变化回调
 */
function u() {
  wx.onBLECharacteristicValueChange(function(e) {
    console.log("characteristic " + e.characteristicId + " has changed, now is " + w.arrayBuffer2HexString(e.value)),
      m(w.arrayBuffer2HexString(e.value));
  });
}
/**
 * 
 */
function d(e, c, o) {
  var i = e;
  void 0 === c && (c = function(e) {}), m = c, v(i, 0, c, o);
}
/**
 * 
 */
function v(e, c, o, i) {
  var n, s = !1;
  e.length > c + 40 ? n = e.substr(c, 40) : (s = !0, n = e.substr(c)), f(n, function(i) {
    "ok" == i && ("ok" != i || s ? (console.log("finish"), console.log(g), console.log(g.deviceId),
      wx.setStorageSync("currentDevice", g), o("success")) : (console.log("nofinish" + s),
      v(e, c + 40)));
  }, i);
}
/**
 * 执行写入命令
 */
function f(e, c, o) {
  var i = e;
  console.log("commond ：" + i);
  var n = w.hexString2ArrayBuffer(i);
  console.log("执行指令:" + w.arrayBuffer2HexString(n)), wx.writeBLECharacteristicValue({
    deviceId: g.deviceId + "",
    serviceId: C.uuid + "",
    characteristicId: h.uuid + "",
    value: n,
    success: function(e) {
      console.log("发送指令成功" + e.errMsg), c("ok");
    },
    fail: function(e) {
      console.log(e), console.log("执行指令失败" + e.errMsg), o("执行指令失败");
    }
  });
}

/**
 * 定义一堆变量
 */
var g = {},
  I = {},
  C = {},
  h = {},
  B = {},
  m = void 0,
  U = void 0,
  w = require("../strUtil.js"),
  S = (require("../crc.js"), !1);

/**
 * 
 */
module.exports = {
  writeCommend: function(c) {
    var o = {
      name: "",
      adviceId: "",
      sendCommend: "",
      onSuccessCallBack: function(e) {},
      onFailCallBack: function(e) {},
      onCompleteCallBack: function(e) {},
      services: [],
      writeServiceUUID: "",
      notifyServiceUUID: "",
      notifyCharacteristicUUID: "",
      writeCharacteristicUUID: ""
    };
    e(Object.assign(o, c)), s();
  },
  closeBLEConnection: function(e) {
    wx.closeBLEConnection({
      deviceId: g.deviceId + "",
      success: function(e) {
        console.log(e);
      }
    });
  },
  openBluetoothAdapter: e
};

var D, k = !1;